package com.ma.home;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Toast;

import com.ma.R;
import com.ma.fees.FeesActivity;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionYes;

import base.BaseActivity;
import utils.ConstantUtil;

public class ChooseRoomActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_room);
    }

    public void ok(View view) {

        startActivity(FeesActivity.class);
    }
    public void scan(View v) {
        boolean isCam= ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED;
         if (isCam){
            AndPermission.with(this)
                    .requestCode(ConstantUtil.permission_code_cam)
                    .permission(Manifest.permission.CAMERA)
                    .send();
            return;
        }

        Intent intent = new Intent( this, CaptureActivity.class);
        startActivityForResult(intent, ConstantUtil.REQUEST_CODE_SCAN);


    }

    //6.0
    @PermissionYes(ConstantUtil.permission_code_cam)
    private void permissionYes() {
        Intent intent = new Intent(this, CaptureActivity.class);
        startActivityForResult(intent, ConstantUtil.REQUEST_CODE_SCAN);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ConstantUtil.REQUEST_CODE_SCAN) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText( this, result, Toast.LENGTH_LONG).show();


                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText( this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

}
